-
Notifications
You must be signed in to change notification settings - Fork 257
btrfs-progs: offline filesystem resize feature #1007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: devel
Are you sure you want to change the base?
Conversation
Will this allow for resizing a fs that would normally go ro due to enospc during mount? |
Yeah, I don't see why not! |
Unfortunately not that simple. If the fs has metadata full, it may still fail. Thankfully this one is only mostly modifying chunk tree, which is not that common to exhaust system chunks. Please use chunk root instead since you're only modifying chunk tree. Furthermore there are a lot of code style problems, like unexpected new lines splitting variable definitions (e.g. between And the parameter list for that function is also pretty bad. I'd say normally we put more important parameter (like path, which specifies a whole fs) first, and more specific parameters like new size after that. |
And a lot of error handling is missing. E.g. if |
Ahh yeah, that makes sense.
Good catch... I don't have the best understanding of btrfs transactions. When I was testing this patch it appeared like the filesystem was being resized correctly, why would this be the case if I'm passing the wrong target? Was it making changes to the wrong tree? Was I just getting lucky?
Will do.
Will check this in v2.
I'll rethink the parameter ordering. I pass the amount parameter as a char because I call check_offline_resize_args inside offline_resize and I wanted to keep all of the argument logic together. |
Ok, makes sense will fix in v2. |
The target root for
You can check how other codes in btrfs-progs is doing. There are exceptions like the existing In your particular case, we won't support anything other than a number, thus parsing it early will be a more common solution. BTW, for your update, you can just force push the same branch. No need to create a new PR. |
In this case there is a difference between passing "+1G" and "1G" that would be lost if I just passed a u64. "+1G" increases the filesystem size by 1G and "1G" sets the filesystem size to 1G. I could pass a u64 along with a boolean to indicate whether the size is relative to the existing filesystem size, but it feels like this logic would be better encapsulated inside check_offline_resize_args. |
An offline tool that increases a device's size--and does nothing else--is much more likely to succeed in that situation than mount + fi-resize, but it can still fail in one really specific case. mount does a number of things that can result in enospc failure. Off the top of my head: orphan inode reclaim, the snapshot cleaner thread, tearing down an incomplete reloc tree, and resuming a balance (this is the only one that can be turned off by a mount option). Avoiding these other tasks makes success far more likely. The chunk tree is stored in the system chunk, which is a dedicated contiguous storage area that is usually already large enough for two copies of the chunk tree. To run out of space there, you'd need hundreds of thousands of chunks, like a Users can run into this case fairly often when they have filesystems that are close to this threshold size (100 TiB or multiples thereof). A smaller filesystem doesn't allocate enough chunks to require a new system chunk, while a bigger filesystem would have unallocated space when it needs a new system chunk. It also comes up if you're running any striped profile (raid0, raid10, raid5, or raid6) and you don't do something to reduce dev_extent fragmentation--I hit this with a 26T filesystem once, that had 250k chunks after several drive upgrades and naive balances. If you're willing to accept overwriting a metadata page in place, you can resize a device by finding the page in the chunk tree where the device item is, changing the size field in the dev item, and updating the csum (in addition to the superblock changes, which are already overwrite-in-place, and repeated across all mirrors). Increasing or decreasing a device size (without relocating any chunks) doesn't require any new allocations. |
Just use |
d7e49b7
to
44e653a
Compare
Force pushed with a v2
I'm still passing the amount as a cstring because a s64 doesn't convey the difference between "+1G" and "1G". |
/* For target sizes without +/- sign prefix (e.g. 1:150g) */ | ||
if (mod == 0) { | ||
new_size = diff; | ||
} else if (mod > 0) { |
Check warning
Code scanning / CodeQL
Comparison result is always the same Warning
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bot is right here. mod should be a bool, it's only ever 0 or 1
f660864
to
a452b1e
Compare
@adam900710 ping for v2 review |
b62465f
to
f4799f1
Compare
This patch introduces the ability to resize a btrfs filesystem while it is not mounted via a new `--offline` flag. Currently only increasing the size of the filesystem is supported, though I believe it would be possible to implement shrinking the filesystem to the end of the last device extent. This is a more general, and hopefully more useful, solution to the problem I was trying to solve with the ("btrfs-progs: add slack space for mkfs --shrink") patch. This patch should enable users to resize a filesystem without the higher capabilities needed for mounting a filesystem. Signed-off-by: Leo Martins <[email protected]> --- Changelog: v1->v2: - use chunk root instead of fs root - fix offline resize error handling - fix variable declarations to not have newlines - fix parameter list to have more important arguments first
f4799f1
to
7a8c4e5
Compare
return 0; | ||
} | ||
|
||
static int offline_resize(const char *path, const char *amount) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just return bool, if you don't particularly care about the actual return value? It'd simplify some of the code.
(bool is a relatively recent addition to C, so there's still a historic tendency to use int for the same thing in some places)
|
||
root = open_ctree(path, 0, OPEN_CTREE_WRITES | OPEN_CTREE_CHUNK_ROOT_ONLY); | ||
if (!root) { | ||
error("could not open file at %s\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might not even need to print an error here, it looks like open_ctree is already quite chatty. But if you did keep it I think you need to make it clear it works with block devices as well as files
goto close; | ||
} | ||
|
||
ret = snprintf(dev_name, BTRFS_VOL_NAME_MAX, "%s", device->name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sprintf with %s is a code smell - that's just a more complicated strcpy. strncpy is the particular function you're looking for here.
But I'd just move it off the stack and use strdup, that way you don't have to worry about it being too long. Don't forget to free it later.
btrfs_abort_transaction(trans, ret); | ||
goto close; | ||
} | ||
ret |= btrfs_commit_transaction(trans, root); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for |=, ret is guaranteed to be 0 here
|
||
if (path_is_reg_file(dev_name)) | ||
ret = truncate(dev_name, new_size); | ||
if (ret) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd move this check into the scope of the if on line 1520
|
||
if (devstr && !dev_found) { | ||
/* Devid specified but not found. */ | ||
error("cannot find devid: %lld", devid); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
devid is unsigned, this should be %llu. The -Wformat-signedness warning will diagnose this
new_size = 0; | ||
} else { | ||
new_size = stat_buf.st_size; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit-picking, but the usual kernel style is that you wouldn't need curly brackets here
/* For target sizes without +/- sign prefix (e.g. 1:150g) */ | ||
if (mod == 0) { | ||
new_size = diff; | ||
} else if (mod > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bot is right here. mod should be a bool, it's only ever 0 or 1
|
||
if (new_size < 256 * SZ_1M) | ||
warning("the new size %lld (%s) is < 256MiB, this may be rejected by kernel", | ||
new_size, pretty_size_mode(new_size, UNITS_DEFAULT)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
%llu
warning("the new size %lld (%s) is < 256MiB, this may be rejected by kernel", | ||
new_size, pretty_size_mode(new_size, UNITS_DEFAULT)); | ||
|
||
pr_verbose(LOG_DEFAULT, "Resize device id %lld from %s to %s\n", devid, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
%llu
This patch introduces the ability to resize a btrfs filesystem while it is not mounted via a new
--offline
flag. Currently only increasing the size of the filesystem is supported, though I believe it would be possible to implement shrinking the filesystem to the end of the last device extent.This is a more general, and hopefully more useful, solution to the problem I was trying to solve with the
("btrfs-progs: add slack space for mkfs --shrink") patch. This patch should enable users to resize a filesystem without the higher capabilities needed for mounting a filesystem.